package com.kjtpay.design_pattern.chain;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.kjtpay.design_pattern.R;
import com.kjtpay.design_pattern.adapter.AdapterActivity;

public class ChainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    public static void launch(Context context) {
        if (null != context) {
            Intent intent = new Intent();
            intent.setClass(context, ChainActivity.class);
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }

    CommonManager commonManager;
    Majordomo majordomo;
    GeneralManager generalManager;
    Request request;
    RadioGroup typeGroup;
    EditText editTextContent;
    EditText editTextNumber;
    TextView textViewResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chain);
        setTitle("职责链模式");

        // 配置职责链
        commonManager = new CommonManager("经理");
        majordomo = new Majordomo("总监");
        generalManager = new GeneralManager("总经理");
        commonManager.setSuperior(majordomo);
        majordomo.setSuperior(generalManager);

        // 请求对象
        request = new Request();

        // 单选按钮
        typeGroup = findViewById(R.id.radioGroupType);
        typeGroup.setOnCheckedChangeListener(this);

        // 内容，数量
        editTextContent = findViewById(R.id.editTextContent);
        editTextNumber = findViewById(R.id.editTextNumber);

        // 响应结果
        textViewResponse = findViewById(R.id.textViewResponse);
    }

    public void onRequestClick(View view) {
        // 根据选择设定请求类型
        switch (typeGroup.getCheckedRadioButtonId()) {
            case R.id.radioButtonDuty:
                request.type = "请假";
                break;
            case R.id.radioButtonMoney:
                request.type = "加薪";
                break;
            default:
                // 什么也不做
                break;
        }
        request.content = editTextContent.getText().toString();
        request.number = Integer.valueOf(editTextNumber.getText().toString());
        commonManager.requestApplication(request);

        String response = commonManager.show();
        commonManager.reset();
        if (null != majordomo.show()) {
            response += majordomo.show();
            majordomo.reset();
        }
        if (null != generalManager.show()) {
            response += generalManager.show();
            generalManager.reset();
        }
        textViewResponse.setText(response);
    }

    // 根据单选，默认填充请求内容
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.radioButtonDuty:
                editTextContent.setText("小菜请假");
                break;
            case R.id.radioButtonMoney:
                editTextContent.setText("小菜请求加薪");
                break;
            default:
                // 什么也不做
                break;
        }
    }
}